#!/usr/bin/env python
# encoding: utf-8

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class SubscribeTableView(QTableView):
    selectRow = 0
    singal_subscribe = pyqtSignal(list)

    def __init__(self, parent=None):
        '''表格初始化'''
        super(QTableView, self).__init__(parent)
        self.HeaderList = ['主题', '服务质量']
        self.model = QStandardItemModel(0, 0)
        self.model.setHorizontalHeaderLabels(self.HeaderList)  #
        self.setModel(self.model)
        # 下面代码让表格100填满窗口
        self.horizontalHeader().setStretchLastSection(True)
        self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.isClearChooseLine_Flag = False
        # 点击选中
        self.clicked.connect(self.handleItemClicked)

    def handleItemClicked(self, item):
        self.selectRow = item.row()

    def clear_data(self):
        self.model.clear()
        self.model = QStandardItemModel(0, 0)
        self.model.setHorizontalHeaderLabels(self.HeaderList)  #
        self.setModel(self.model)

    def deleteOneTopic(self):
        self.model.removeRow(self.selectRow)

    def addOneTopic(self, topic, qos):
        """添加一条订阅"""
        rows = self.model.rowCount()  # 总行数
        self.model.setItem(rows, 0, QStandardItem(topic))
        self.model.setItem(rows, 1, QStandardItem(qos))
    
    def getRows(self):
        rows = self.model.rowCount()  # 总行数
        cols = self.model.columnCount()  # 总行数
        data = []
        for r in range(0, rows):
            row_data = []
            for c in range(0, cols):
                row_data.append(self.model.data(self.model.index(row, 0)))
            data.append(tmp)
        return data

    def getTopics(self):
        rows = self.model.rowCount()  # 总行数
        topics = []
        for i in range(rows):
            topic = self.model.data(self.model.index(i, 0))
            qos = int(self.model.data(self.model.index(i, 1)))
            topics.append((topic, qos))
        return topics

